#include "DragWindow.h"

namespace DinoUI
{
	DragWindow::DragWindow(void)
	{
		m_bEnableDrag = false;
		m_bIsDragged = false;
		m_bHorizontalDragEnable = true;
		m_bVerticalDragEnable = true;
	}


	DragWindow::~DragWindow(void)
	{
	}

	bool DragWindow::HandleEvent(WidgetEvent *evt)
	{
		if (IsActive() == false)
			return false;

		// handle events
		switch(evt->eventType)
		{
		case WIDGETEVENT_TOUCH:
			if(IsPointInside(evt->x, evt->y))
			{
				if(m_bIsDragged == false)
				{
					m_bEnableDrag = true;
					//m_bIsDragged = true;
					m_dragX = evt->x;
					m_dragY = evt->y;
				}
			}
			
			break;

		case WIDGETEVENT_UNTOUCH:
			if(m_bIsDragged) 
			{
				m_bIsDragged = false;
				m_bEnableDrag = false;
				return true;
			}
			
			break;

		case WIDGETEVENT_DRAG:
			if(IsPointInside(evt->x, evt->y) == false)
				break;

			if(m_bIsDragged == true)
			{
				int dispX, dispY;

				// horizontal drag
				if(m_bHorizontalDragEnable)
					dispX = evt->x - m_dragX;
				else
					dispX = 0;

				// vertical drag
				if(m_bVerticalDragEnable)
					dispY = evt->y - m_dragY;
				else
					dispY = 0;

				m_dragX = evt->x;
				m_dragY = evt->y;

				SetPosition(GetX() + dispX, GetY() + dispY);
				return true;
			}
			else
			{
				if(m_bEnableDrag)
					m_bIsDragged = true;
			}
			break;
		}

		// event not handled
		if(Window::HandleEvent(evt) == true)
			return false;
		else
			return true;
	}

	void DragWindow::EnableHorizontalDrag(bool enable)
	{
		m_bHorizontalDragEnable = enable;
	}

	void DragWindow::EnableVerticalDrag(bool enable)
	{
		m_bVerticalDragEnable = enable;
	}
}
